<?php
require_once ('initialize.php');
include_once(Utils::getPath(array('shared', 'template.php')));
include_once(Utils::getPath(array('repositories', 'userRepository.php')));
include_once(Utils::getPath(array('summary.php')));

$template = new Template('application');
$template->initialize();

$iduser = $_SESSION['authenticatedId'];

$userRep = new UserRepository();
$user = $userRep->getById($iduser);
$friendsCount = $userRep->countFriends($user->getId(), true);
$referer = getenv("HTTP_REFERER");

$username = $user->getUserName();
$name = $user->getName();
$email = $user->getEmail();
$image = $user->getImage();
$description = $user->getDescription();

$news = Summary::getMyProfile($user->getId());
?>

<h2><?php echo $username; ?></h2>
<div id="profile-info">
    <div><span>Name: </span><?php echo $name; ?></div>
    <div><span>Email: </span><?php echo $email; ?></div>
</div>
<div class="profile-description"><p><?php echo $description; ?></p></div>
<h3>
    Latest activities of your friends:
</h3>
<div id="news">
    <?php for ($i = 0; $i < count($news); $i++) { ?>
        <div class="new-element">
            <div class="new-image"><a href="<?php echo Utils::getUrl('user/profile.php?id='.$news[$i]['userId']) ?>"><img alt="User Avatar" src ="<?php echo $news[$i]['image'] ?>" /></a></div>
            <div class="new-text"><?php echo $news[$i]['message'] ?></div>
            <?php if ($news[$i]['url'] != null) { ?>
            <div class="new-url"><a href="<?php echo $news[$i]['url'] ?>">View</a></div>
            <?php } ?>
            <div class="new-date"><?php echo Utils::ago($news[$i]['datetime']) ?></div>        
        </div>
        <?php } 
        if (count($news) == 0) { ?>
        <div>There is nothing new to show..</div>
        <?php if ($friendsCount == 0) { ?>
        <div>You don't have any friend, you can search them in 'People' tab in top-menu.</div>
        <?php }
        } ?>
</div>
<?php
    $template->createPage("Profile $username");
?>